PROGRAM wormhole;
GLOBAL
shipid;

starcounter=0;

approxfps=0;

shipspeed;
shipangle;
shipx;
shipy;
spacepressed;

targetid;
targetx;
targety;

checkingid;

BEGIN
load_fpg("bh.fpg");
set_mode(800600);
set_fps(60,0);

resolution=10;

shipid=ship();

start_scroll(0,0,12,11,0,15);
scroll.z=5000;

ctype=c_scroll;

write_int(0,750,10,4,&approxfps);
    write(0,150,10,4,"Target ID");
write_int(0,150,30,4,&targetid);
write_int(0,400,180,4,&spacepressed);
    write(0,400,170,4,"Spacebar Pressed");
write_int(0,400,200,4,&shipangle);
    write(0,380,200,5,"Ships path angle");
write_int(0,400,210,4,&shipspeed);
    write(0,380,210,5,"Ships speed");

LOOP

approxfps=(fps/6)*6;
spacepressed=key(_space);

IF (starcounter<60)
    starcounter=starcounter+1;
    map_put_pixel(0,11,rand(1,360),rand(1,360),rand(3,15));
    map_put_pixel(0,12,rand(1,360),rand(1,360),rand(3,15));
    refresh_scroll(0);
    IF (starcounter>30)
        enemy();
    END
ELSE
    FRAME;
END

IF (key(_esc))
    exit("Coward",0);
END

END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS ship();
PRIVATE
speed_x;
speed_y;
movespeed=0;

addangle=400;

shotcounter;
adcounter;
targetchecker;

shotside=-1;

life;

BEGIN
x=5120;
y=3840;
resolution=10;
graph=3;

ctype=c_scroll;

shipdisp();
targetarrow();

targetid=get_id(TYPE enemy);

LOOP

checkingid=get_id(TYPE enemy);
IF (get_dist(checkingid)<get_dist(targetid) AND key(_r_shift));
    targetid=checkingid;
END

IF (checkingid==0)
    FRAME;

scroll.camera=id;
scroll.x1=x/14;
scroll.y1=y/14;

shipx=x;
shipy=y;

movespeed=abs(speed_x)+abs(speed_y);

x+=(speed_x/10);
y+=(speed_y/10);
advance(movespeed/30);

shotcounter=shotcounter+1;
adcounter=adcounter+1;

signal(TYPE angledisp,s_kill);
angledisp(speed_x,speed_y);

IF (key(_up))
    IF (rand(1,3)<2)
        graph=7;
    ELSE
        graph=rand(1,2);
    END
    IF (movespeed<1300)
        speed_x+=get_distx(angle,10);
        speed_y+=get_disty(angle,10);
    END
ELSE
    graph=7;
END

IF (key(_down))
    speed_x=(speed_x*24)/25;
    speed_y=(speed_y*24)/25;
END

IF (key(_right));
    IF (rand(1,3)<2)
        graph=7;
    ELSE
        graph=rand(5,6);
    END
    angle=angle-addangle;
    addangle=addangle+200;
END

IF (key(_left))
    IF (rand(1,3)<2)
        graph=7;
    ELSE
        graph=rand(3,4);
    END
    angle=angle+addangle;
    addangle=addangle+200;
END

IF (key(_left) AND key(_right))
    IF (rand(1,3)<2)
        graph=7;
    ELSE
        graph=rand(1,2);
    END
END

IF (NOT (key(_left) OR key(_right)))
    addangle=800;
END

IF (addangle>7000)
    addangle=7000;
END

IF (speed_x>0)
    speed_x=speed_x-1;
END

IF (speed_y>0)
    speed_y=speed_y-1;
END

IF (speed_x<0)
    speed_x=speed_x+1;
END

IF (speed_y<0)
    speed_y=speed_y+1;
END

IF (spacepressed==1 AND shotcounter>4)
    shotcounter=0;
    shot(shotside,movespeed);
    shotside=shotside*-1;
END

END

END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS shipdisp();
BEGIN
resolution=10;
z=father.z-1;
graph=31;

LOOP
FRAME;

x=(father.x/20)+3744;
y=(father.y/20)+2808;

END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS angledisp(speed_x,speed_y);
PRIVATE
speed_x;
speed_y;
movespeed;

BEGIN
size=1;
resolution=10;
x=father.x;
y=father.y;
angle=father.angle;

ctype=c_scroll;

movespeed=abs(speed_x)+abs(speed_y);

x+=speed_x*3;
y+=speed_y*3;
advance(movespeed);

graph=31;

LOOP
shipangle=(fget_angle(x,y,shipx,shipy)/1000);
shipspeed=(fget_dist(x,y,shipx,shipy)/11);
FRAME;

END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS shot(side,speed);
PRIVATE
life=100;
side;
speed;

BEGIN
resolution=10;
x=father.x;
y=father.y;
angle=father.angle;
advance(350);
graph=21;

ctype=c_scroll;

IF (side==1)
xadvance(angle-90000,50);
END

IF (side==-1)
xadvance(angle+90000,50);
END

LOOP
FRAME;

life=life-1;
advance(250+(speed/10));

IF (life<1)
    BREAK;
END

END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS enemy();
PRIVATE
targeted=0;

targid;

life=1;

kind=1;

movespeed;
speed_x;
speed_y;

BEGIN
resolution=10;
x=rand(-200000,200000);
y=rand(-200000,200000);

ctype=c_scroll;



IF (kind==1)
    graph=101;
    life=20;
END



LOOP
FRAME;

IF (targetid==id)
    targetx=x;
    targety=y;

    IF (targeted==0)
        targid=targetor();
        targeted=1;
    END

ELSE
    targeted=0;
    signal(targid,s_kill);
END

IF (collision(TYPE shot))
    life=life-1;
END



IF (kind==1)

    graph=rand(101,103);
    angle=near_angle(angle,fget_angle(x,y,shipx,shipy),2200);

    IF (life<1)
        explosion(angle,50,60);
        enemy();
        BREAK;
    END

    movespeed=abs(speed_x)+abs(speed_y);

    x+=(speed_x/15);
    y+=(speed_y/15);
    advance(movespeed/20);

    IF (movespeed<1100)
        speed_x+=get_distx(angle,10);
        speed_y+=get_disty(angle,10);
    END

    IF (speed_x>0)
        speed_x=speed_x-1;
    END

    IF (speed_y>0)
        speed_y=speed_y-1;
    END

    IF (speed_x<0)
        speed_x=speed_x+1;
    END

    IF (speed_y<0)
        speed_y=speed_y+1;
    END

END



END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS targetor();
PRIVATE
flagscounter;

BEGIN
resolution=10;
size=100;
flags=4;

ctype=c_scroll;

LOOP
IF (father.x==0 AND father.y==0 AND father.angle==0)
    BREAK;
ELSE
    flagscounter=flagscounter+1;
    FRAME;
    graph=131;
END

IF (flagscounter>11)
    flagscounter=0;
    IF (flags==4)
        flags=0;
    ELSE
        flags=4;
    END
END

x=father.x;
y=father.y;
angle=father.angle;
size=45;

END
END




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS targetarrow();
PRIVATE
flagscounter;

BEGIN
resolution=10;
flags=4;

ctype=c_scroll;

LOOP
FRAME;

graph=14;
x=father.x;
y=father.y;
z=father.z-1;

IF ((angle/6000)==(fget_angle(x,y,targetx,targety))/6000)
    flagscounter=flagscounter+1;
ELSE
    flags=4;
END

IF (get_id(TYPE targetor)<>0)
    angle=near_angle(angle,fget_angle(x,y,targetx,targety),4000);
END

IF (flagscounter>11)
    flagscounter=0;
    IF (flags==4)
        flags=0;
    ELSE
        flags=4;
    END
END

END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS explosion(angle,speed,size);
BEGIN
x=father.x;
y=father.y;
resolution=10;
graph=201;

size=size/2;

ctype=c_scroll;

LOOP
FRAME;

advance(speed/2);

graph=graph+1;

IF (graph>236)
    BREAK;
END

END
END



